/**
 * 
 */
package org.jint.adsltest.analysis;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * @author jint
 * 
 */
public class Analysis {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Analysis01();

	}

	/**
	 * 简单的统计出loadtime的分布
	 */
	private static void Analysis01() {
		Connection conn = null;
		PreparedStatement stmt = null;
		// PreparedStatement stmt2 = null;
		String sql = "";
		int suiteId = 1;
		ArrayList<PageTestSuite> suites = new ArrayList<PageTestSuite>();
		try {
			conn = ConnectionUtil.getCon();
			Statement stat = conn.createStatement();

			strUtil.printInfo("Get TestSuite. ");
			sql = "select id,place,roundNo from PageTestSuite where status='download OK'";
			ResultSet rs = stat.executeQuery(sql);
			while (rs.next()) {

				int id = rs.getInt("id");
				String place = rs.getString("place");
				String roundNo = rs.getString("roundNo");

				suites.add(new PageTestSuite(id, place, roundNo, 64));
			}
			rs.close();
			stat.close();
			strUtil.printInfo("Get " + suites.size() + " Test.");

			sql = "select loadtime from PageTestDetail where suiteId=?";
			stmt = conn.prepareStatement(sql);

			for (int i = 0; i < suites.size(); i++) {
				PageTestSuite suite = suites.get(i);
				stmt.setInt(1, suite.id);
				rs = stmt.executeQuery();
				while (rs.next()) {
					float loadtime = rs.getFloat("loadtime");
					// strUtil.printInfo("loadtime:" + loadtime );
					suite.level(loadtime);
				}
				rs.close();
				// strUtil.printInfo("Suite Analysis01 : " + suite);
			}
			stmt.close();

			conn.setAutoCommit(false);
			strUtil.printInfo("Save analysis result :" + suites.size());
			sql = "INSERT INTO Analysis01 (suiteId,place,roundNo,"
					+ "case_count,not_access,not_test,data_error,"
					+ "level_0_5,level_5_10,level_10_20,level_20_30,"
					+ "level_30_60,level_60_120,level_120_more) "
					+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			stmt = conn.prepareStatement(sql);
			for (int i = 0; i < suites.size(); i++) {
				PageTestSuite suite = suites.get(i);
				stmt.setInt(1, suite.id);
				stmt.setString(2, suite.place);
				stmt.setString(3, suite.roundNo);
				stmt.setInt(4, suite.case_count);
				stmt.setInt(5, suite.not_access);
				stmt.setInt(6, suite.not_test);
				stmt.setInt(7, suite.data_error);
				stmt.setInt(8, suite.level_0_5);
				stmt.setInt(9, suite.level_5_10);
				stmt.setInt(10, suite.level_10_20);
				stmt.setInt(11, suite.level_20_30);
				stmt.setInt(12, suite.level_30_60);
				stmt.setInt(13, suite.level_60_120);
				stmt.setInt(14, suite.level_120_more);

				stmt.executeUpdate();
			}
			stmt.close();

			sql = "UPDATE PageTestSuite SET status='analysis OK' WHERE id=?";
			stmt = conn.prepareStatement(sql);
			for (int i = 0; i < suites.size(); i++) {
				PageTestSuite suite = suites.get(i);
				stmt.setInt(1, suite.id);
				stmt.executeUpdate();
			}
			stmt.close();

			try {
				conn.commit();
			} catch (SQLException e) {
				e.printStackTrace();
				conn.rollback(); // 操作不成功，回滚事务
			}

			strUtil.printInfo("Save analysis result Success!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} // 结束数据库的连接
		}
	}
}
